package com.bytedance.common.utility.io;

import com.bytedance.common.utility.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class NetHackDbg {
    public enum ImageType {
        UNKNOWN,
        JPG,
        PNG,
        GIF
    }


    private static final byte[] a;
    private static final byte[] b;
    private static final byte[] c;
    private static final byte[] d;

    static {
        a = new byte[]{71, 73, 70, 56, 55, 97};
        b = new byte[]{71, 73, 70, 56, 57, 97};
        c = new byte[]{-1, -40, -1};
        d = new byte[]{-119, 80, 78, 71, 13, 10, 26, 10};
    }

    public static InputStream a(String arg3) {
        InputStream v0 = null;
        File v2 = new File(arg3);
        if (v2.exists()) {
            try {
                v0 = new FileInputStream(v2);
            } catch (Exception v1) {
                v1.printStackTrace();
            }
        }

        return v0;
    }

    public static boolean a(File arg2) {
        return NetHackDbg.b(arg2) == ImageType.GIF;
    }

    public static boolean a(InputStream arg6, String arg7, String arg8) {
        int v1_2;
        FileOutputStream v3;
        File v1_1;
        FileOutputStream v2 = null;
        boolean v0 = false;
        if (arg6 == null) {
            return v0;
        }

        try {
            v1_1 = new File(arg7);
            if (!v1_1.exists() && !v1_1.mkdirs()) {
            } else {
                v3 = new FileOutputStream(new File(v1_1, arg8));
                v1_2 = 1024;
                byte[] v1_3 = new byte[v1_2];
                while (true) {
                    int v4 = arg6.read(v1_3);
                    if (v4 == -1) {
                        break;
                    }
                    v3.write(v1_3, 0, v4);
                }
                v3.flush();
            }
        } catch (Exception v1) {
            v1.printStackTrace();
        } finally {
            if (arg6 != null) {
                try {
                    arg6.close();
                } catch (Exception v1) {
                }
            }
        }
        return v0;
    }

    public static boolean a(String arg4, String arg5, String arg6) {
        FileInputStream v2 = null;
        boolean v0 = false;
        if (android.support.design.a.isTextEmpty(arg4)) {
            return v0;
        }

        if (android.support.design.a.isTextEmpty(arg5)) {
            return v0;
        }

        if (android.support.design.a.isTextEmpty(arg6)) {
            return v0;
        }

        FileInputStream v3 = null;
        try {
            v2 = new FileInputStream(arg4);
            v0 = NetHackDbg.a(((InputStream) v2), arg5, arg6);
        } catch (Exception v1) {
            v1.printStackTrace();
        } finally {
            try {
                v2.close();
            } catch (Exception v1) {
            }
        }
        return v0;
    }

    private static boolean a(byte[] arg5, byte[] arg6) {
        boolean v1 = false;
        if (arg6 != null) {
            int v0 = 0;
            while (true) {
                if (v0 >= arg6.length) {
                    break;
                } else if (arg5[v0] == arg6[v0]) {
                    ++v0;
                    continue;
                }

                return v1;
            }

            v1 = true;
        }

        return v1;
    }

    private static ImageType b(File arg4) {
        int v2;
        FileInputStream v1_1 = null;
        ImageType v0;
        if (arg4 != null && (arg4.exists())) {
            v0 = ImageType.UNKNOWN;
            try {
                v1_1 = new FileInputStream(arg4);
                v2 = 8;
                byte[] v2_2 = new byte[v2];
                v1_1.read(v2_2);
                if (!NetHackDbg.a(v2_2, NetHackDbg.b) && !NetHackDbg.a(v2_2, NetHackDbg.a)) {
                    if (NetHackDbg.a(v2_2, NetHackDbg.c)) {
                        v0 = ImageType.JPG;
                    } else {
                        if (NetHackDbg.a(v2_2, NetHackDbg.d)) {
                            v0 = ImageType.PNG;
                        } else {
                            return v0;
                        }
                    }
                } else {
                    v0 = ImageType.GIF;
                    return v0;
                }

                return v0;
            } catch (Exception e) {

            } finally {
                try {
                    v1_1.close();
                } catch (Exception v1) {
                }
            }
            return v0;
        } else {
            v0 = ImageType.UNKNOWN;
        }

        return v0;
    }

    public static ImageType b(String arg1) {
        ImageType imageType = !android.support.design.a.isTextEmpty(arg1) ? NetHackDbg.b(new File(arg1)) : ImageType.UNKNOWN;
        return imageType;
    }

    public static void c(String arg5) {
        try {
            Class v0_5 = Class.forName("android.os.FileUtils");
            if (v0_5 == null) {
                return;
            }

            Method v0_6 = v0_5.getMethod("setPermissions", String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE);
            if (v0_6 == null) {
                return;
            }

            int v0_7 = ((Integer) v0_6.invoke(null, arg5, Integer.valueOf(420), Integer.valueOf(-1), Integer.valueOf(-1))).intValue();
            if (v0_7 == 0) {
                return;
            }
            Logger.d("NetHackDbg", "android.os.FileUtils.setPermissions() returned " + v0_7 + " for \'" + arg5 + "\', probably didn\'t work.");
        } catch (ClassNotFoundException v0) {
            Logger.d("NetHackDbg", "android.os.FileUtils.setPermissions() failed - ClassNotFoundException.");
        } catch (IllegalAccessException v0_1) {
            Logger.d("NetHackDbg", "android.os.FileUtils.setPermissions() failed - IllegalAccessException.");
        } catch (InvocationTargetException v0_2) {
            Logger.d("NetHackDbg", "android.os.FileUtils.setPermissions() failed - InvocationTargetException.");
        } catch (NoSuchMethodException v0_3) {
            Logger.d("NetHackDbg", "android.os.FileUtils.setPermissions() failed - NoSuchMethodException.");
        } catch (Throwable v0_4) {
        }
    }
}

